package uit.uthuoc.activity;

import uit.uthuoc.fragment.SearchResultFragment;
import uit.uthuoc.fragment.SearchResultFragmentInterface;

import com.uit.uthuoc.R;

import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.SearchView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;


public class MainActivity extends SingleFragmentActivity implements SearchResultFragmentInterface{
	
	private static final String LOG_ID = "uit.uThuoc.activity.MainActivity";
	
	SearchResultFragment mChildFragment;
	
	@Override
	protected Fragment createFragment() {
		return mChildFragment;
	}
	
	@Override
	protected boolean isUpButtonEnable() {
		// TODO Auto-generated method stub
		return false;
	}
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
		mChildFragment = new SearchResultFragment();
        Log.d(LOG_ID, "onCreate called, fragment instance was created.");
        super.onCreate(savedInstanceState);
        
    }
    
    @Override
    protected void onNewIntent(Intent intent) {
    	super.onNewIntent(intent);
    	
//		Log.d(LOG_ID, "onNewIntent called.");
//    	
//    	// This itent is for searching
//        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
//        	mChildFragment.onSearchButtonClicked();
//        }
    }


    @Override
	protected void onDestroy() {
		super.onDestroy();
		Log.d(LOG_ID, "onDestroy called.");
	}

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
//        // Inflate the menu; this adds items to the action bar if it is present.
//        getMenuInflater().inflate(R.menu.main_activity_actions, menu);
//        
//        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
//        MenuItem searchItem = menu.findItem(R.id.action_search);
//        SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
//        
//        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
//        searchView.setIconifiedByDefault(false);
        
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

	@Override
	public void onListItemClickListener(int position) {
		Log.d(LOG_ID, "Drug " + position + " was clicked");
		Intent intent = new Intent(this, DrugDetailsActivity.class);
		intent.putExtra(DrugDetailsActivity.EXTRA_INTENT_INT_ID, position);
		startActivity(intent);
	}
}
